﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MainScreen : Form
    {
        danquanEntities danquanInfoModel = new danquanEntities();

        public MainScreen()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNew addNewForm = new AddNew(this);
            this.Hide();
            addNewForm.Show();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Dispose();
                this.Close();
            }
        }

        private void MainScreen_Load(object sender, EventArgs e)
        {
            var query = from c in danquanInfoModel.dqinfoes 
                        select c;
            try
            {
                dqinfo1DataGridView.DataSource = query;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kết nối dữ liệu thất bại. Vui lòng kiểm tra.", "Có lỗi!");
                this.Dispose();
                this.Close();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MainScreen_Load(sender, e);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!txtName.Text.Equals(""))
            {
                var query = from c in danquanInfoModel.dqinfoes
                            where c.tenKhaiSinh == txtName.Text && c.DOB.Year == txtDob.Value
                            select c;
                MessageBox.Show(query.Count().ToString());
                dqinfo1DataGridView.DataSource = query;
            }
            else 
            {
                var query = from c in danquanInfoModel.dqinfoes
                            where c.DOB.Year == txtDob.Value
                            select c;
                MessageBox.Show(query.Count().ToString());
                dqinfo1DataGridView.DataSource = query;
            }
            
        }

        private void dqinfo1DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa dòng đang lựa chọn?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // pick selected item
                int selectedID = int.Parse(dqinfo1DataGridView.SelectedRows[0].Cells[0].Value.ToString());

                // delete base on ID
                dqinfo deletedPerson = danquanInfoModel.dqinfoes.First(c => c.idDQInfo == selectedID);
                danquanInfoModel.DeleteObject(deletedPerson);
                danquanInfoModel.SaveChanges();

                // update table
                MainScreen_Load(sender, e);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // pick selected item
            int selectedID = int.Parse(dqinfo1DataGridView.SelectedRows[0].Cells[0].Value.ToString());
            dqinfo chosenPerson = danquanInfoModel.dqinfoes.First(c => c.idDQInfo == selectedID);
            AddNew addNewForm = new AddNew(this, danquanInfoModel, chosenPerson);
            this.Hide();
            addNewForm.Show();
        }
    }
}
